﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    public class ParticleManager
    {
        List<Particle> particleList = new List<Particle>();
        ContentManager Content;
        Random rand = new Random();
        float deff = 0.005f;

        // Particles properties
        Vector2 position, position2;
        float speed;
        float opacity;
        float scale;
        double angle;

        public ParticleManager(ContentManager Content)
        {
            this.Content = Content;
        }

        public void Update()
        {
            ManageParticles();

            angle += deff;

            AddParticle();

            foreach (Particle item in particleList)
                item.Update();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Particle item in particleList)
                item.Draw(spriteBatch);

            spriteBatch.DrawString(PlatformerGame.debugFont, "Angle : " + angle, new Vector2(0, 0), Color.White);
        }

        /* Custom */
        void AddParticle()
        {
            speed = (float)rand.NextDouble() * 3;
            opacity = (float)rand.NextDouble();
            scale = (float)rand.NextDouble() / 5;


            Particle temp = new Particle(Content, position, scale, speed, opacity, RandomAngle());
            particleList.Add(temp);

            speed = (float)rand.NextDouble() * 3;
            opacity = (float)rand.NextDouble();
            scale = (float)rand.NextDouble() / 5;
            

            Particle temp2 = new Particle(Content, position2, scale, speed, opacity, RandomAngle());
            particleList.Add(temp2);
        }

        void ManageParticles()
        {
            if (angle > 6.28f)
                angle = 0f;

            position = new Vector2(PlatformerGame.WIDTH / 2 + (float)Math.Cos(angle + Math.PI + 1) * 750, PlatformerGame.HEIGHT / 2 + (float)Math.Sin(angle + Math.PI + 1) * 750);
            position2 = new Vector2(PlatformerGame.WIDTH / 2 + (float)Math.Cos(angle + 1) * 750, PlatformerGame.HEIGHT / 2 + (float)Math.Sin(angle + 1) * 750);
        }

        double RandomAngle()
        {
            return angle + (rand.NextDouble() * 2);
        }
    }
}
